<?php
/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2005-4-30 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$idx = new promote;
class promote
{
	function promote()
	{
		
		global $BIZ,$INCOME,$CONF,$_LEVEL;
		
		$this -> upload_dir = stripslashes($CONF['upload_dir']);
		$this -> seller_data = new manager($BIZ->seller['seller_id']);

		//---------------------------------------
		// Kill globals - globals bad, Homer good.
		//---------------------------------------
		
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( $tmp_in as $k => $v )	unset($$k);
		
		if ( empty($BIZ->seller['seller_id']) or $BIZ->seller['seller_id']=="" or $BIZ->seller['seller_id']==0 )
		{
			$BIZ->error( "用户没有登陆，不能使用此功能","?act=Login&code=00" );
		}

		if( !$this -> seller_data->check($INCOME[act]) )
            $BIZ -> error("该等级用户不享有该功能。");
	    //--------------------------------------------
        // Check viewing permissions, etc
	    //--------------------------------------------
						
		if ( empty($BIZ->seller['seller_id']) or $BIZ->seller['seller_id']=="" or $BIZ->seller['seller_id']==0 )
		{
			$BIZ->error( "用户没有登陆，不能使用此功能","?act=Login&code=00" );
		}
	    if( $BIZ->seller['seller_check_status']=='uncheck' )
		{
			$this -> control_panel("nocharge");
		}elseif( ($data=$this -> seller_data->get_seller_data("end_time,property_id"))&& $data['end_time']<time()&&$_LEVEL[$data['property_id']][1]>0 )
		{
			$this -> control_panel("due");
		}else
		{
			$BIZ -> assign('code',$INCOME['code']);
			$BIZ-> nav_link[] =array ("商家控制台","index.php?act=Promote");
			switch($INCOME['code'])
			{
				case '00':     // display general seller info
				   $this -> control_panel();
				   break;
				case '01':
					$BIZ -> nav_link[] = array("自助商业推广");
					$this -> popular_form();
				    break;
				case 'viewpool1':
				   $BIZ -> nav_link[] = array("首页排队待推广商品");
				   $this -> view_nona_goods();   // none active goods
				   break;
				case 'viewpool2':
					$BIZ -> nav_link[] = array("正在首页的推广商品");
					$this -> view_agoods();       // active goods
				    break;
				case 'viewpool3':
					$BIZ -> nav_link[] = array("排队未推广店铺");
					$this -> view_nona_shop();
				   break;
				case 'viewpool4':
					$BIZ -> nav_link[] = array("正在推广店铺");
					$this -> view_ashop();
				    break;
				case 'extend':
					$this -> extend_form();
				   break;
				case 'asp':
					$this -> add_shop_popular();
				   break;
				case 'doasp':
					$this -> do_asp();   //do add shop popular
				   break;
				case 'doextend':
					$this -> do_extend();
				  break;
				case 'idx':
					$BIZ -> nav_link[] = array("商品首页推荐","");
					$this -> index_goods_form();
				   break;
				case 'doidx':
					$this -> do_index_goods();
				   break;
				case 'delo':
					$this -> del_order();
				    break;
				case 'bid':
					$this -> bid_form();  //竟价历史列表
				default:
				  $this -> control_panel();
			}
		}
		$BIZ -> output('Promote.tpl');
	}
	function control_panel($msg='')    
	{  // 00 , default
		global $BIZ,$DB;
         
		//------------------------------
		//fill display stuff here
        //------------------------------
        if($msg=='nocharge')
		     $BIZ ->assign("notice","您目前还<font class=blue size=+2>未付费</font>，所有功能暂时停止。请点击<a href='?act=User&code=buy'>[<font class=red>这里</font>]</a>付费开通。");
		elseif($msg=='due')
			$BIZ ->assign("notice","您目前购买等级<font class=blue size=+2>已到期</font>，所有功能暂时停止。请点击<a href='?act=User&code=buy'>[<font class=red>这里</font>]</a>续费开通。");

		$DB -> db_query("SELECT count(*) as ct FROM biz_goods WHERE seller_id = ".$BIZ ->seller['seller_id']." AND goods_mark_sell='0' UNION ALL SELECT count(*) as ct FROM biz_shop WHERE seller_id = ".$BIZ ->seller['seller_id']);
		$r_g = $DB -> db_fetch_row();
		$BIZ -> assign("gnum",$r_g['ct']);

		$r_s = $DB -> db_fetch_row();
		$BIZ -> assign("snum",$r_s['ct']);

		$DB -> db_query("SELECT count(*) as ct from biz_message  JOIN biz_goods ON seller_id = ".$BIZ ->seller['seller_id']."  AND message_owner_goods_id = goods_id AND message_reply_content IS NULL");
        $r_m = $DB -> db_fetch_row();
		$BIZ -> assign("mnum",$r_m['ct']);
		//----------------------------------
		//fill recent user property info
		//----------------------------------
		$BIZ -> assign("prop",$this -> seller_data -> build_property_info());
		return;
	}
	function popular_form()
	{//37  一些统计信息
		global $DB,$BIZ,$POSTER;

		$DB -> db_query("SELECT count(*) as ct FROM biz_order_pool WHERE seller_id=".$BIZ->seller[seller_id]."      AND active=0 AND type=".$POSTER[index_goods][type]." UNION ALL SELECT count(*) as ct FROM biz_order_pool WHERE seller_id=".$BIZ->seller[seller_id]." AND active=1 AND type=".$POSTER[index_goods][type]." UNION ALL SELECT count(*) as ct FROM biz_order_pool WHERE seller_id=".$BIZ->seller[seller_id]." AND active=0 AND type<>".$POSTER[index_goods][type]." UNION ALL SELECT count(*) as ct FROM biz_order_pool WHERE seller_id=".$BIZ->seller[seller_id]." AND active=1 AND type<>".$POSTER[index_goods][type]);
		while($r = $DB -> db_fetch_row())
		{
			$ct[] = array($r[ct]);
		}
		$BIZ -> assign("ct",$ct);
	}
	function view_nona_goods()
	{   // 非激活列队商品
		global $DB,$BIZ;
		$DB -> db_query("SELECT bg.goods_name, bp.* FROM biz_order_pool AS bp,biz_goods AS bg WHERE bg.goods_id = bp.goods_id AND bg.goods_id<>0 AND bp.seller_id =".$BIZ -> seller['seller_id']." AND bp.active=0 ");
		while($r = $DB -> db_fetch_row() )
		{
			$tm = date( "y-m-d H:i",$r[order_time] );
			$vng[] = array($r[goods_name],$tm,$r[order_allow_count],$r[order_length],$r[id]);
		}
		$BIZ -> assign("vng",$vng);
		return;
	}
    
	function view_agoods()
	{  //激活列队商品
	   global $DB,$BIZ;
	   $DB -> db_query("SELECT bg.goods_name, bp.* FROM biz_order_pool AS bp,biz_goods AS bg WHERE bg.goods_id = bp.goods_id AND bg.goods_id<>0 AND bp.seller_id =".$BIZ -> seller['seller_id']." AND active<>0 ");
	   
		 while($r = $DB -> db_fetch_row() )
		 {
		   $tm = date( "y-m-d H:i",$r[order_time] );
		   $tm2 = date(  "y-m-d H:i",$r[end_time]);
		  $vg[] = array($r[goods_name],$tm,$r[order_allow_count],$r[order_length],$r[id],$tm2);
		 }
		$BIZ -> assign("vg",$vg);
		return;
	}

	function extend_form()
	{
		global $DB,$BIZ,$INCOME;
		
		$oid=intval($INCOME['ORDER_ID']);

		$DB -> db_query("SELECT * FROM biz_order_pool WHERE id=".$oid." LIMIT 1");
		$o = $DB -> db_fetch_row();
		$o[end_time] = date( "y-m-d H:i",$o[end_time]);
		$BIZ -> assign("o",$o);
	    return;
	}

	function do_extend()
	{
		global $DB,$BIZ,$INCOME,$feedb;

		$add = intval($INCOME['add_length'])*3600*24;
        $id = intval($INCOME['ORDER_ID']); 
		$DB -> db_query("SELECT type FROM biz_order_pool WHERE id=".$id);
		$result = $DB -> db_fetch_row();
		if($result['type']==1)
		{
			 $flag = "index_shop";
			 $flagstr = "首页商铺延期";
		}elseif($result['type']==2)
		{
			 $flag ="index_goods";
			 $flagstr = "首页商品续期";
		}
		elseif($result['type']==3)
		{
			 $flag = "second_shop";
			 $flagstr = "二级商铺续期";
		}
		$money = $feedb[$flag]*intval($INCOME['add_length']);
		$res = $this -> seller_data ->consume($money);
		if(  $res<0 ) $BIZ -> error("提示：您的可用消费金额已不足！你还差{$res}点");
		$this -> seller_data -> consume_log($money,$flagstr,$BIZ ->seller[seller_id]);

		$DB -> db_query(" UPDATE biz_order_pool SET end_time = end_time+ ".$add." , order_allow_count = order_allow_count-1 WHERE id=".$id." AND order_allow_count >0" );
		if(!$DB -> db_affected_rows() )
			$BIZ -> error("提示，您已超出允许的续期次数。");
    	$BIZ ->redirect_screen("恭喜！您已经成功地延长了推荐结束日期。共消费{$money}点","?act=Promote&code=01");
	    return;
	}
	function view_nona_shop()
	{// 非激活列队商铺
		global $DB,$BIZ,$POSTER;
		$DB -> db_query("SELECT bs.shop_name, bp.* FROM biz_order_pool AS bp,biz_shop AS bs WHERE bs.shop_id = bp.shop_id AND bs.shop_id<>0 AND bp.seller_id =".$BIZ -> seller['seller_id']." AND bp.active=0 ");
		while($r = $DB -> db_fetch_row() )
		{
			$tm = date( "y-m-d H:i",$r[order_time] );
			$vns[] = array($r[shop_name],$tm,$r[order_allow_count],$r[order_length],$r[id],$POSTER[$r[type]][name]);
		}
		$BIZ -> assign("vns",$vns);
		return;
	}
	function view_ashop()
	{
		//激活列队商品
	   global $DB,$BIZ,$POSTER;
	   $DB -> db_query("SELECT bs.shop_name, bp.* FROM biz_order_pool AS bp,biz_shop AS bs WHERE bs.shop_id = bp.shop_id AND bs.shop_id<>0 AND bp.seller_id =".$BIZ -> seller['seller_id']." AND active<>0 ");
	   
		 while($r = $DB -> db_fetch_row() )
		 {
		   $tm = date( "y-m-d H:i",$r[order_time] );
		   $tm2 = date(  "y-m-d H:i",$r[end_time]);
		  $vs[] = array($r[shop_name],$tm,$r[order_allow_count],$r[order_length],$r[id],$tm2,$POSTER[$r[type]][name]);
		 }
		$BIZ -> assign("vs",$vs);
		return;
	}
	function add_shop_popular()
	{
		global $DB,$BIZ,$INCOME,$POSTER;
		$sid = intval($INCOME['SHOP_ID']);
		//-------------检查此商品是否以设定-------------------
		$DB -> db_query("SELECT * FROM biz_order_pool WHERE shop_id =".$sid);
		if($DB -> db_fetch_num())
			$BIZ -> error("提示：您已为该商铺设定首页推广");
		//-----------------------
		$DB -> db_query("SELECT shop_name,shop_id FROM biz_shop WHERE shop_id=".$sid);
		$r = $DB -> db_fetch_row();
        
		$BIZ -> assign( "tid",array("index_shop","second_shop") );
		$BIZ -> assign( "tname",array("首页推荐商铺","二级页面商铺推荐") );
		$BIZ -> assign("name",$r[shop_name]);
		$BIZ -> assign("id",$r[shop_id]);
		return;
	}
	function do_asp()  //do add shop popular
	{
		global $DB,$BIZ,$INCOME,$feedb,$POSTER;
		 $lengh = intval($INCOME['oder_length']);
		 $type = $INCOME['type'];
		 $tid =$POSTER[$type][type];
		 $money =$feedb[$type] *$lengh;
		 $res = $this -> seller_data ->consume($money);
		 if(  $res<0 ) $BIZ -> error("提示：您的可用消费金额已不足！你还差{$res}点");
         $this -> seller_data -> consume_log($money,"商铺推广",$BIZ ->seller[seller_id]);
		 $sid = intval($INCOME['SHOP_ID']);

		 if(!$lengh) $BIZ -> error("错误！持续天数至少为1天");
		 $count = $this -> seller_data -> get_order_count();
		 $tm = time();
		 $sql = "INSERT INTO `biz_order_pool` (`shop_id`, `seller_id`, `type`, `order_time`,  `order_allow_count`, `order_length`) VALUES ('{$sid}','{$BIZ->seller[seller_id]}', '{$tid}', '{$tm}', '{$count}', '{$lengh}')";
		 $DB -> db_query($sql);
		 $BIZ -> update_repute('idx_shop_add');
         $BIZ ->redirect_screen("恭喜！您已经成功地把您商铺加入推广排队序列。消费".$money."点","?act=Promote&code=01");
	}
	function index_goods_form()
	{
		global $BIZ,$INCOME,$DB;
        $id = intval($INCOME['GOODS_ID']);
		
		//-------------检查此商品是否以设定-------------------
		$DB -> db_query("SELECT * FROM biz_order_pool WHERE goods_id =".$id);
		if($DB -> db_fetch_num())
			$BIZ -> error("你以设定该商品为首页推广");
		//-----------------------
		
		$DB -> db_query("SELECT goods_name, goods_id FROM biz_goods WHERE goods_id=".$id." LIMIT 1");
		$g = $DB -> db_fetch_row();
		
		$BIZ -> assign("name",$g['goods_name']);
		$BIZ -> assign("id",$g['goods_id']);
		return;

	}
	function do_index_goods()
	{//doidx
	     global $BIZ,$INCOME,$DB,$feedb,$POSTER;
		 $lengh = intval($INCOME['oder_length']);
		 $money = $feedb['index_goods']*$lengh;
		 $res = $this -> seller_data ->consume($money);
		 if( $res<0 ) $BIZ -> error("提示：您的可用消费金额已不足！你还差{$res}点");
         $this -> seller_data -> consume_log($money,"购买商品首页推广",$BIZ ->seller[seller_id]);
		 $gid = intval($INCOME['GOODS_ID']);
		 
		 if(!$lengh) $BIZ -> error("错误！持续天数至少为1天");
		 $count = $this -> seller_data -> get_order_count();
		 $tm = time();
		 $sql = "INSERT INTO `biz_order_pool` (`goods_id`, `seller_id`, `type`, `order_time`,  `order_allow_count`, `order_length`) VALUES ('{$gid}','{$BIZ->seller[seller_id]}', '{$POSTER[index_goods][type]}', '{$tm}', '{$count}', '{$lengh}')";
		 $DB -> db_query($sql);
 	    $BIZ -> update_repute('idx_goods_add');
		 $BIZ ->redirect_screen("恭喜！您已经成功地把您的一件商品加入首页推广排队序列。消费".$money."点","?act=Promote&code=16");
	}
	function del_order()
	{
		global $DB,$BIZ,$INCOME;
        $id = intval($INCOME['ORDER_ID']);
        $DB -> db_query( "DELETE FROM  biz_order_pool WHERE id =".$id );
    	$BIZ ->redirect_screen("恭喜！您已经成功删除了一条推广记录。","?act=Promote&code=01");
	    return;
	}
	function bid_form()
	{
		global $DB,$BIZ,$bid_status;
		$DB -> db_query("SELECT p.*,g.goods_name FROM biz_bid_pool p LEFT JOIN biz_goods g ON p.goods_id=g.goods_id WHERE p.seller_id=".$BIZ->seller['seller_id']." ORDER BY p.bid_time DESC");
		while($rs = $DB -> db_fetch_row() )
		{
			$bid[] = array($rs[goods_name],$rs[bid_time],$rs[bid_price],$rs[order_length],$bid_status[$rs[state]]);
		}
		$BIZ -> assign('bid',$bid);
	}
}
?>